//
//  ViewController.m
//  UI_21_NSURLRequesAndRwsponse
//
//  Created by li cheng on 16/8/11.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // NSURL 负责请求的地址
    NSURL *baiduURL = [NSURL URLWithString:@"http://www.baidu.com/user/wanghuang/1.html?userName=xxx&pwdxxx"];
    //; 完整的URL对应的字符串
    NSLog(@"baidu URL.absoluteString: %@", baiduURL.absoluteString);
    // host: 主机地址/域名
    NSLog(@"baiduURL.host: %@", baiduURL.host);
    // 3.path: 主机地址后对应的路径
    NSLog(@"baiduURL.past: %@", baiduURL.path);
    // 4.fragment:
    NSLog(@"baiduURL.fragment: %@", baiduURL.fragment);
    // 5. query:? 以后参数的值
    NSLog(@"baiduURL: %@", baiduURL.query);
    NSLog(@"%@", baiduURL.resourceSpecifier);
    // 不可变请求 和 可变请求
    // 1.NSURLRequest
    NSURLRequest *baiDu = [NSURLRequest requestWithURL:baiduURL];
    // 不可变的
    NSLog(@"old:%@", baiDu.allHTTPHeaderFields);
    NSMutableURLRequest *baiduMutableRequst = [NSMutableURLRequest requestWithURL:baiduURL];
    // 1> 超时时间, 一个请求持续多久, 会失败
    baiduMutableRequst.timeoutInterval = 60;
    // 2> HTTPMethod 请求的只是 GET POST PUT DELETE
    baiduMutableRequst.HTTPMethod = @"GET";
    
    // 3> allHTTOHeaderFields 设置网路请求头
    [baiduMutableRequst setValue:@"iOS 10.0 / iPhone8.1" forHTTPHeaderField:@"User-Agent"];
    // 获取对应的头领域
    [baiduMutableRequst valueForHTTPHeaderField:@"User-Agent"];
    // 根据字典特性后填入的值会覆盖啊
    NSLog(@"%@", baiduMutableRequst.allHTTPHeaderFields);
    // 4> HTTPBoby (一般POST请求时,才会使用
    // 一般情况下, 都是字典/字符串转换成Data
    baiduMutableRequst.HTTPBody = [NSData data];
    // 一般情况下, 都是字典/字符串转换成Data
    baiduMutableRequst.HTTPBody = [NSData data];
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:baiduURL statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"data" : [NSData data]}];
    // 1> statusCode 状态码
    // response.statusCode
    // 2> allHeaderFields 响应头
    // 内容类型
    // Type : applicaton/json; charset:utf-8
    // 时间 日期
    // 缓存的一些内容
    NSLog(@"%@", response.allHeaderFields);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
